#include	<ncAppleTalk.h>
#include	<memory.h>

XPPParmBlkPtr	*XPPh;

short		i;

Handle		SCBh;

long		li,asyncf;

doHelp()
{
	printf("\n");
	printf("T_OpenSession ASPTimeOut ASPRetry Net Node Socket AsyncFlag\n");
	printf("	Returns -	ioResult,\n");
	printf("				Session Ref. #,\n");
	printf("				Handle to SCB,\n");
	printf("				Handle to IOPB\n");
	printf("	SCB & IOPB memory handles are locked until CloseSession or AbortOS called.\n");
	exit(0);
}

showrslts()
{
	if (XPPh != 0)
		{
			printf("\n");
			printf("ioResult       = %d\n",(*XPPh)->xppPrmBlk.ioResult);
			printf("Session Ref.   = %d\n",(*XPPh)->xppPrmBlk.SessRefnum);
			printf("SCB Handle     = %ld\n",SCBh);
			printf("IOPB Handle    = %ld\n",XPPh);
		}
}

abort(str)
Ptr		str;
{
	showrslts();
	printf("\n");
	printf("### Error - %s\n",str);
	if (XPPh != 0)
	{
		li = (*XPPh)->xppPrmBlk.ioResult;
		DisposHandle(XPPh);
	}
	else
		li = 0;
	if (SCBh != nil)
		DisposHandle(SCBh);
	if (li != 0)
		exit(li);
	else
		exit(255);
}

main(argc,argv)
int		argc;
Ptr		argv[];
{
	SCBh = nil;
	XPPh = nil;
		
	if (argc <= 1)
		exit(0);
	if (EqualString(argv[1],"HELP",false,false))
		doHelp();
	if (argc != 7)
		abort("Not Enough Parameters, try HELP.");
	
	if (MPPOpen())
		abort("Cannot open .MPP driver.");

	SetZone(SystemZone());
	XPPh = NewHandle(sizeof(XPPParamBlock));
	HLock(XPPh);
	
	SCBh = NewHandle(SCBMemSize);
	HLock(SCBh);
	SetZone(ApplicZone());
	
	(*XPPh)->xppPrmBlk.ioResult = OpenXPP(&i);
	if ((*XPPh)->xppPrmBlk.ioResult)
		abort("Could not open XPP driver.");
	(*XPPh)->xppPrmBlk.ioRefNum = i;
	(*XPPh)->xppPrmBlk.ioCompletion = 0;
	StringToNum(argv[1],&li);
	(*XPPh)->xppPrmBlk.ASPTimeout = li;
	StringToNum(argv[2],&li);
	(*XPPh)->xppPrmBlk.ASPRetry = li;
	StringToNum(argv[3],&li);
	(*XPPh)->aspOpenPrm.ServerAddr.aNet = li;
	StringToNum(argv[4],&li);
	(*XPPh)->aspOpenPrm.ServerAddr.aNode = li;
	StringToNum(argv[5],&li);
	(*XPPh)->aspOpenPrm.ServerAddr.aSocket = li;
	StringToNum(argv[6],&asyncf);
	(*XPPh)->aspOpenPrm.SCBPointer = *SCBh;
	(*XPPh)->aspOpenPrm.AttnRoutine = 0;
	
	(asyncf) ? (ASPOpenSession(*XPPh,true)) : (ASPOpenSession(*XPPh,false));
		
	showrslts();
	exit((*XPPh)->xppPrmBlk.ioResult);
}
